package org.eraser.security.authentication.oidc;

import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.*;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * 从数据库查询出用户信息
 */
@Service
public class EndUserService implements UserDetailsService, UserDetailsPasswordService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        LambdaQueryWrapper<UserEntity> userQueryWrapper = new LambdaQueryWrapper<>();
//        userQueryWrapper.eq(UserEntity::getAccountName, username);
//        UserEntity userEntity = UserEntity.builder().build().selectOne(userQueryWrapper);

        ArrayList<SimpleGrantedAuthority> roles = new ArrayList<>();
        // return new UserSubject(userEntity.getAccountName(), userEntity.getAccountPassword(), roles);
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        roles.add(new SimpleGrantedAuthority("admin"));
        User user = new User("123", bCryptPasswordEncoder.encode("123"), roles);

        return EndUser.builder().user(user).build();
    }

    @Override
    public UserDetails updatePassword(UserDetails user, String newPassword) {

        return null;
    }
}
